package com.vasthu.gui;

import com.vasthu.api.domain.Category;
import com.vasthu.api.domain.Item;
import com.vasthu.api.service.CategoryManagerService;
import com.vasthu.api.service.ItemManagerService;
import com.vasthu.core.service.CategoryManagerServiceImpl;
import com.vasthu.core.service.ItemManagerServiceImpl;
import com.vasthu.gui.utility.SpringUtilities;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;

public class VstItemDataForm extends JDialog {
	
	private long itemId;
    private JLabel lItemTitle;
    private JLabel lItemCode;
    private JLabel lItemName;
    private JLabel lItemDesc;
    private JLabel lItemBrand;
    private JLabel lItemCatId;
    private JTextField tItemCode;
    private JTextField tItemName;
    private JTextField tItemDesc;
    private JTextField tItemBrand;
    private JComboBox tItemCatId;
	private Component spacer;
    private JButton addItem;
    private JButton updateItem;
    private JButton deleteItem;
    private JButton cancelButton;
	private int tableRow;
	private VstTableCallBack callback;
	private long currentItemId;
	
	private ItemManagerService itemManagerService = null;
	private CategoryManagerService categoryManagerService = null;
	
	public void setItemId(int itemId) {
		this.itemId = itemId;
	}
	
	public long getItemId() {
		return itemId;
	}

    public VstItemDataForm(int mode, int tableRow, VstTableCallBack callback) {
		itemManagerService = new ItemManagerServiceImpl();
		categoryManagerService = new CategoryManagerServiceImpl();
		
        initUi();
		this.tableRow = tableRow;
		this.callback = callback;
		currentItemId = -1;
		
		switch(mode) {
			case VstGuiConstants.ITEM_FORM_ADD:
				addItem.setVisible(true);
                updateItem.setVisible(false);
                deleteItem.setVisible(false);
				spacer.setVisible(false);
				break;
			case VstGuiConstants.ITEM_FORM_UPDATE:
				addItem.setVisible(false);
                updateItem.setVisible(true);
                deleteItem.setVisible(true);
				spacer.setVisible(true);
				break;
			default:
				break;
		}
		
    }

    private void initUi() {
        setTitle("Item");
        
        lItemTitle = new JLabel("Item", JLabel.CENTER);
        lItemTitle.setFont(new Font("Monospaced 12", Font.BOLD, 16));

        lItemCode = new JLabel("Code : ", JLabel.TRAILING);
        lItemCode.setFont(new Font("Monospaced 12", Font.PLAIN, 12));
		
		lItemName = new JLabel("Name : ", JLabel.TRAILING);
        lItemName.setFont(new Font("Monospaced 12", Font.PLAIN, 12));

        lItemDesc = new JLabel("Description : ", JLabel.TRAILING);
        lItemDesc.setFont(new Font("Monospaced 12", Font.PLAIN, 12));

        lItemBrand = new JLabel("Brand : ", JLabel.TRAILING);
        lItemBrand.setFont(new Font("Monospaced 12", Font.PLAIN, 12));

        lItemCatId = new JLabel("Category : ", JLabel.TRAILING);
        lItemCatId.setFont(new Font("Monospaced 12", Font.PLAIN, 12));

        tItemCode = new JTextField();
        tItemCode.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tItemCode.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
		
		tItemName = new JTextField();
        tItemName.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tItemName.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
		
        tItemDesc = new JTextField();
        tItemDesc.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tItemDesc.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
		
        tItemBrand = new JTextField();
        tItemBrand.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tItemBrand.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));
        
        tItemCatId = new JComboBox();
        java.util.List<String> catNames = categoryManagerService.getAllCategoryNames();
		
		for(String name : catNames) {
			tItemCatId.addItem(name);
		}

        tItemCatId.setPreferredSize(new Dimension(Integer.MAX_VALUE, 25));
        tItemCatId.setMaximumSize(new Dimension(Integer.MAX_VALUE, 25));

		addItem = new JButton("ADD");
        addItem.setPreferredSize(new Dimension(100, 40));
        addItem.setMaximumSize(new Dimension(100, 40));
        updateItem = new JButton("UPDATE");
        updateItem.setPreferredSize(new Dimension(100, 40));
        updateItem.setMaximumSize(new Dimension(100, 40));
        deleteItem = new JButton("DELETE");
        deleteItem.setPreferredSize(new Dimension(100, 40));
        deleteItem.setMaximumSize(new Dimension(100, 40));
        cancelButton = new JButton("CANCEL");
        cancelButton.setPreferredSize(new Dimension(100, 40));
        cancelButton.setMaximumSize(new Dimension(100, 40));

        JPanel basePanel = new JPanel();
        basePanel.setLayout(new BoxLayout(basePanel, BoxLayout.Y_AXIS));
        basePanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 10));

        JPanel topPanel = new JPanel(new FlowLayout());
        topPanel.add(lItemTitle);
        topPanel.setPreferredSize(new Dimension(Integer.MAX_VALUE, 30));
        topPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 30));
        basePanel.add(topPanel);

        JPanel dataPanel = new JPanel(new SpringLayout());

        dataPanel.add(lItemCode);
        dataPanel.add(tItemCode);
        dataPanel.add(lItemName);
        dataPanel.add(tItemName);
        dataPanel.add(lItemDesc);
        dataPanel.add(tItemDesc);
        dataPanel.add(lItemBrand);
        dataPanel.add(tItemBrand);
        dataPanel.add(lItemCatId);
        dataPanel.add(tItemCatId);
        basePanel.add(dataPanel);
        
        SpringUtilities.makeCompactGrid(dataPanel,
                5, 2, //rows, cols
                8, 8, //initialX, initialY
                0, 8);

        basePanel.add(Box.createVerticalGlue());

        JPanel mainButtonPanel = new JPanel();
		mainButtonPanel.setLayout(new BoxLayout(mainButtonPanel, BoxLayout.X_AXIS));
		mainButtonPanel.add(Box.createHorizontalGlue());
        mainButtonPanel.add(addItem);
        mainButtonPanel.add(updateItem);
		spacer = Box.createRigidArea(new Dimension(10, 10));
		mainButtonPanel.add(spacer);
        mainButtonPanel.add(deleteItem);
		mainButtonPanel.add(Box.createRigidArea(new Dimension(10, 10)));
        mainButtonPanel.add(cancelButton);
        basePanel.add(mainButtonPanel);

		add(basePanel);
		
		initActions();
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setMinimumSize(new Dimension(600, 310));
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width - 600) / 2, (screen.height - 310) / 2);
    }

    private void initActions() {

        addItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Item item = getItem();
				itemManagerService.insertItem(item);
            }
        });

        updateItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                
            }
        });

        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
				itemManagerService.deleteItemById(currentItemId);
				// DB row must be deleted before this line
                callback.rowDeleted(tableRow);
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                close();
            }
        });
    }
	
	public Item getItem() {
		Item item = new Item();
		
		item.setItemCode(tItemCode.getText());
		item.setItemName(tItemName.getText());
		item.setItemDesc(tItemDesc.getText());
		item.setItemBrand(tItemBrand.getText());
		
		String catName = (String)tItemCatId.getSelectedItem();
		Category cat = categoryManagerService.getCategoryByName(catName);
		item.setItemCategoryId(cat.getCategoryId());
		
		return item;
	}
	
	public void populate(Item item) {
		if (item == null)
			return;
		
		currentItemId = item.getItemId();
		tItemCode.setText(item.getItemCode());
		tItemName.setText(item.getItemName());
		tItemDesc.setText(item.getItemDesc());
		tItemBrand.setText(item.getItemBrand());
		
		Category cat = categoryManagerService.getCategoryById(item.getItemCategoryId());
		if (cat != null) {
			tItemCatId.setSelectedItem(cat.getCategoryName());
		}
	}

    private void close() {
        dispose();
    }
	
}